﻿#include "NetworkHostInfo.h"
#include "ui_NetworkHostInfo.h"

#include <QHostInfo>
#include <QHostAddress>
#include <QNetworkInterface>
#include <QNetworkAddressEntry>

NetworkHostInfo::NetworkHostInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NetworkHostInfo)
{
    ui->setupUi(this);
}

NetworkHostInfo::~NetworkHostInfo()
{
    delete ui;
}

QString NetworkHostInfo::protocalName(QAbstractSocket::NetworkLayerProtocol protocol)
{
    switch(protocol)
    {
    case QAbstractSocket::IPv4Protocol:{
        return "IPv4 Protocol";
    }break;
    case QAbstractSocket::IPv6Protocol:{
        return "IPv6 Protocol";
    }break;
    case QAbstractSocket::AnyIPProtocol:{
        return "Any IP Protocol";
    }

    default:{
        return "Unknown Network Layer Protocol";
    }
    }
}



void NetworkHostInfo::on_btnGetLocal_clicked()
{
    // QHostInfo获取主机信息

    // 本地主机
    QString hostName = QHostInfo::localHostName();

    ui->plainTextEdit->appendPlainText(QStringLiteral("本机主机名：") + hostName + "\n");

    // 本机Ip地址
    QHostInfo hostInfo = QHostInfo::fromName(hostName);

    // IP地址列表
    QList<QHostAddress> addrList = hostInfo.addresses();

    for(int i = 0; i < addrList.count(); ++i)
    {
        // 每一项都是一个QHostAddress
        QHostAddress aHost = addrList.at(i);

        bool show = ui->checkBox->isChecked();
        if(show)
        {
            show = (QAbstractSocket::IPv4Protocol == aHost.protocol());
        }
        else
        {
            show = true;
        }

        if(show)
        {
            ui->plainTextEdit->appendPlainText(QStringLiteral("协议:") + protocalName(aHost.protocol()));
            ui->plainTextEdit->appendPlainText(QStringLiteral("本机IP地址:") + aHost.toString());
            ui->plainTextEdit->appendPlainText("");
        }
    }

}

void NetworkHostInfo::on_btnClear_clicked()
{
    ui->plainTextEdit->clear();
}

// 查找主机信息
void NetworkHostInfo::on_btnSearch_clicked()
{
    QString hostName = ui->edtYuMing->text();
    ui->plainTextEdit->appendPlainText(QStringLiteral("正在查找主机信息：") + hostName);
    QHostInfo::lookupHost(hostName, this, &NetworkHostInfo::slotLookupHostInfo);
}


void NetworkHostInfo::slotLookupHostInfo(const QHostInfo &hostInfo)
{
    QList<QHostAddress> addrList = hostInfo.addresses();

    for(int i = 0; i < addrList.count(); ++i)
    {
        QHostAddress aHost = addrList.at(i);
        bool show = ui->checkBox->isChecked();
        if(show)
        {
            show = (QAbstractSocket::IPv4Protocol == aHost.protocol());
        }
        else
        {
            show = true;
        }

        if(show)
        {
            ui->plainTextEdit->appendPlainText(QStringLiteral("协议") + protocalName(aHost.protocol()));
            ui->plainTextEdit->appendPlainText(aHost.toString());
        }
    }
}

void NetworkHostInfo::on_btnAllInterface_clicked()
{
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();

    for(int i = 0; i < list.count(); ++i)
    {
        QNetworkInterface aInterface = list.at(i);
        if(!aInterface.isValid())
            continue;

        ui->plainTextEdit->appendPlainText(QStringLiteral("设备名称:") + aInterface.humanReadableName());
        ui->plainTextEdit->appendPlainText(QStringLiteral("硬件地址:") + aInterface.hardwareAddress());

        QList<QNetworkAddressEntry> entryList = aInterface.addressEntries();
        for(int j = 0; j < entryList.count(); ++j)
        {
            QNetworkAddressEntry aEntry = entryList.at(j);
            ui->plainTextEdit->appendPlainText(QStringLiteral("IP地址:") + aEntry.ip().toString());
            ui->plainTextEdit->appendPlainText(QStringLiteral("子网掩码:") + aEntry.netmask().toString());
            ui->plainTextEdit->appendPlainText(QStringLiteral("广播地址:") + aEntry.broadcast().toString());
        }
        ui->plainTextEdit->appendPlainText("\n");
    }
}

void NetworkHostInfo::on_btnAllAddress_clicked()
{
    QList<QHostAddress> addrList = QNetworkInterface::allAddresses();

    for(int i = 0; i < addrList.count(); ++i)
    {
        QHostAddress aHost = addrList.at(i);

        bool show = ui->checkBox->isChecked();

        if(show)
        {
            show = (QAbstractSocket::IPv4Protocol == aHost.protocol());
        }
        else
        {
            show = true;
        }

        if(show)
        {
            ui->plainTextEdit->appendPlainText(QStringLiteral("协议:") + protocalName(aHost.protocol()));
            ui->plainTextEdit->appendPlainText(QStringLiteral("IP地址:") + aHost.toString());
            ui->plainTextEdit->appendPlainText("\n");
        }
    }
}
